<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of AdvSearchController
 *
 * @author srkrishnan
 */
class AdvSearchControllerCore extends FrontController {

    public $instantSearch;
    public $ajaxSearch;

    public function __construct() {
        $this->php_self = 'advsearch.php';

        parent::__construct();

        $this->instantSearch = Tools::getValue('instantSearch');
        $this->ajaxSearch = Tools::getValue('ajaxSearch');
    }

    public function preProcess() {
        parent::preProcess();

        $query = Tools::getValue('query_color');

        $getinfo = $_GET;
        $query = array();
        $advcategoryid = null;
        foreach ($getinfo as $key => $value) {
            $k = $key;
            $v = $value;
            $attribid = Tools::getValue($k);
            if (($attribid != -1 && ($k != "orderby" && $k != "orderway") && is_numeric($attribid) && $this->startsWith($k,"attrib_")) || ($k == "query_color" && $attribid != -1) || ($k == "advcategoryid" && $attribid != -1) ) {
                if($k == "advcategoryid")
                {
                    $advcategoryid = $attribid;
                }
                else
                    $query[] = $attribid;
            }
        }

        if (isset($query)) {
            $this->productSort();
            $this->n = abs((int) (Tools::getValue('n', Configuration::get('PS_PRODUCTS_PER_PAGE'))));
            $this->p = abs((int) (Tools::getValue('p', 1)));
            $search = AdvSearch::find((int) (self::$cookie->id_lang), $query,$advcategoryid, $this->p, $this->n, $this->orderBy, $this->orderWay);
            Module::hookExec('advsearch', array('expr' => $query, 'total' => $search['total']));
            $nbProducts = $search['total'];
            $this->pagination($nbProducts);
            
           
            //custom edit
             $disco = array();
             $sear_products = $search['result'];
             foreach($sear_products as $prid) {
                      $pr=$prid['id_product'];
                      $disco[$pr]=Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS('

		SELECT iconname
                FROM ' . _DB_PREFIX_ . 'discoicons
                where productid='.$pr);
				}
                        //custom edit
            
            self::$smarty->assign(array(
                'discoimg' => isset($disco)?$disco:NULL,
                'products' => $search['result'], // DEPRECATED (since to 1.4), not use this: conflict with block_cart module
                'search_products' => $search['result'],
                'nbProducts' => $search['total'],
                'search_query' => "Attributes",
                'homeSize' => Image::getSize('home')));
        } else {
            self::$smarty->assign(array(
                'products' => array(),
                'search_products' => array(),
                'pages_nb' => 1,
                'nbProducts' => 0));
        }
        self::$smarty->assign('add_prod_display', Configuration::get('PS_ATTRIBUTE_CATEGORY_DISPLAY'));
    }

    public function startsWith($haystack, $needle) {
        $length = strlen($needle);
        return (substr($haystack, 0, $length) === $needle);
    }

    public function displayHeader() {
        if (!$this->instantSearch AND !$this->ajaxSearch)
            parent::displayHeader();
        else
            self::$smarty->assign('static_token', Tools::getToken(false));
    }

    public function displayContent() {
        parent::displayContent();
        self::$smarty->display(_PS_THEME_DIR_ . 'search.tpl');
    }

    public function displayFooter() {
        if (!$this->instantSearch AND !$this->ajaxSearch)
            parent::displayFooter();
    }

    public function setMedia() {
        parent::setMedia();

        if (!$this->instantSearch AND !$this->ajaxSearch)
            Tools::addCSS(_THEME_CSS_DIR_ . 'product_list.css');
    }

}

?>
